package com.sf.asp.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sf.asp.entity.Permission;
import com.sf.asp.dao.PermissionMapper;
import com.sf.asp.entity.RolePermission;
import com.sf.asp.entity.dto.DeleteDTO;
import com.sf.asp.entity.dto.MenuDTO;
import com.sf.asp.entity.dto.MenuSaveOrUpdateDTO;
import com.sf.asp.entity.vo.MenuVO;
import com.sf.asp.entity.vo.PermissionVO;
import com.sf.asp.service.PermissionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sf.asp.service.RolePermissionService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jins
 * @since 2021-11-24
 */
@Service
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission> implements PermissionService {

    private final PermissionMapper permissionMapper;

    private final RolePermissionService rolePermissionService;

    public PermissionServiceImpl(PermissionMapper permissionMapper,RolePermissionService rolePermissionService){
        this.permissionMapper = permissionMapper;
        this.rolePermissionService  = rolePermissionService;
    }

    @Override
    public List<MenuDTO> getUserMenu(String userId) {
        List<Permission> permissions = permissionMapper.selectList(new QueryWrapper<>());
        List<MenuDTO> menus = new ArrayList<>();

        //构造树级目录,避免使用递归
        List<MenuDTO> temp = new ArrayList<>();
        Map<Integer,MenuDTO> map = new HashMap<>(permissions.size());
        for (Permission permission : permissions){
            MenuDTO menuDTO = permissionToMenuDto(permission);
            temp.add(menuDTO);
            map.put(menuDTO.getId(),menuDTO);
        }

        for (MenuDTO data : temp){
            if (data.getPid() != null && data.getPid() != 0){
                map.get(data.getPid()).getChildren().add(data);
            } else {
                menus.add(data);
            }
        }

        return menus;
    }

    @Override
    public List<MenuVO> listMenu() {
        return permissionMapper.listMenu();
    }

    @Override
    public void saveMenu(MenuSaveOrUpdateDTO menu) {
        Permission permission = MenuSaveOrUpdateDTO.voToPermission(menu);
        permissionMapper.insert(permission);
    }

    @Override
    public void editMenu(MenuSaveOrUpdateDTO menu) {
        Permission permission = MenuSaveOrUpdateDTO.voToPermission(menu);
        permissionMapper.updateById(permission);
    }

    @Override
    public void deleteMenu(DeleteDTO delete) {
        permissionMapper.deleteBatchIds(delete.getIds());
    }

    @Override
    public List<MenuVO> menuParents() {
        return permissionMapper.listMenuParents();
    }

    @Override
    public List<MenuVO> getPermissions() {
        return permissionMapper.listMenu();
    }

    @Override
    public List<PermissionVO> getPermissionByRole(Integer roleId) {
        List<Permission> permissions =  permissionMapper.selectList(new QueryWrapper<>());

        List<PermissionVO> permissionVOS = new ArrayList<>();
        for (Permission permission : permissions){
            permissionVOS.add(PermissionVO.permissionToVo(permission));
        }
        List<RolePermission> rolePermissions = rolePermissionService.listByRoleId(roleId);

        if (rolePermissions.isEmpty()){
            return permissionVOS;
        }

        List<Integer> selectPermission = rolePermissions.stream().map(RolePermission::getPermissionId).collect(Collectors.toList());

        permissionVOS.forEach(permissionVO -> {
            if (selectPermission.contains(permissionVO.getId())){
                permissionVO.setCheckArr("1");
            }
        });
        return permissionVOS;
    }

    private MenuDTO permissionToMenuDto(Permission permission){
        MenuDTO menuDTO = new MenuDTO();
        BeanUtil.copyProperties(permission,menuDTO);
        menuDTO.setPid(permission.getParentId());
        return menuDTO;

    }
}
